package com.Artificialintelligence.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.Artificialintelligence.common.core.domain.entity.SysUser;
import com.Artificialintelligence.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.Artificialintelligence.common.annotation.RepeatSubmit;
import com.Artificialintelligence.common.annotation.Log;
import com.Artificialintelligence.common.core.controller.BaseController;
import com.Artificialintelligence.common.core.domain.AjaxResult;
import com.Artificialintelligence.common.core.validate.AddGroup;
import com.Artificialintelligence.common.core.validate.EditGroup;
import com.Artificialintelligence.common.core.validate.QueryGroup;
import com.Artificialintelligence.common.enums.BusinessType;
import com.Artificialintelligence.common.utils.poi.ExcelUtil;
import com.Artificialintelligence.system.domain.vo.SysAuthUserVo;
import com.Artificialintelligence.system.domain.bo.SysAuthUserBo;
import com.Artificialintelligence.system.service.ISysAuthUserService;
import com.Artificialintelligence.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 第三方授权Controller
 *
 * @author ruoyi
 * @date 2021-12-14
 */
@Validated
@Api(value = "第三方授权控制器", tags = {"第三方授权管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/authUser")
public class SysAuthUserController extends BaseController {

    private final ISysAuthUserService iSysAuthUserService;

    /**
     * 查询第三方授权列表
     */
    @ApiOperation("查询第三方授权列表")
    @PreAuthorize("@ss.hasPermi('system:authUser:list')")
    @GetMapping("/list")
    public TableDataInfo<SysAuthUserVo> list(@Validated(QueryGroup.class) SysAuthUserBo bo) {
        return iSysAuthUserService.queryPageList(bo);
    }

    /**
     * 导出第三方授权列表
     */
    @ApiOperation("导出第三方授权列表")
    @PreAuthorize("@ss.hasPermi('system:authUser:export')")
    @Log(title = "第三方授权", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated SysAuthUserBo bo, HttpServletResponse response) {
        List<SysAuthUserVo> list = iSysAuthUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "第三方授权", SysAuthUserVo.class, response);
    }

    /**
     * 获取第三方授权详细信息
     */
    @ApiOperation("获取第三方授权详细信息")
    @PreAuthorize("@ss.hasPermi('system:authUser:query')")
    @GetMapping("/{authId}")
    public AjaxResult<SysAuthUserVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("authId") Long authId) {
        return AjaxResult.success(iSysAuthUserService.queryById(authId));
    }

    /**
     * 新增第三方授权
     */
    @ApiOperation("新增第三方授权")
    @PreAuthorize("@ss.hasPermi('system:authUser:add')")
    @Log(title = "第三方授权", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SysAuthUserBo bo) {
        return toAjax(iSysAuthUserService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改第三方授权
     */
    @ApiOperation("修改第三方授权")
    @PreAuthorize("@ss.hasPermi('system:authUser:edit')")
    @Log(title = "第三方授权", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysAuthUserBo bo) {
        return toAjax(iSysAuthUserService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除第三方授权
     */
    @ApiOperation("删除第三方授权")
    @PreAuthorize("@ss.hasPermi('system:authUser:remove')")
    @Log(title = "第三方授权" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{authIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] authIds) {
        return toAjax(iSysAuthUserService.deleteWithValidByIds(Arrays.asList(authIds), true) ? 1 : 0);
    }
}
